import {
  _decorator,
  Component,
  Node,
  SpriteFrame,
  Animation,
  AnimationClip,
} from "cc";
import { Kit } from "../../Test1/Kits/Kit";
const { ccclass, property } = _decorator;

@ccclass("AnimationTest2")
export class AnimationTest2 extends Component {
  @property
  spriteFrameArray: SpriteFrame[] = [];

  @property
  assetsFolderPath: string;

  @property
  frameRate: number = 10;

  @property
  clipDuration: number = 1;

  @property
  clipWrapMode: number = AnimationClip.WrapMode.Loop;

  @property
  isPlaySign: boolean = true;

  @property
  animationName = "AnimationFromAnimationTest2";

  clipName = "ClipFromAnimationTest2";
  anim: Animation;

  async getSpriteArray(): Promise<SpriteFrame[]> {
    const kit = Kit.getInstance();
    let res = await kit.loadImages(this.assetsFolderPath);
    return res;
  }

  async getClip(): Promise<AnimationClip> {
    this.spriteFrameArray = await this.getSpriteArray();

    let clip = AnimationClip.createWithSpriteFrames(
      this.spriteFrameArray,
      this.frameRate
    );

    clip.name = this.clipName;
    clip.duration = this.clipDuration;
    clip.wrapMode = this.clipWrapMode;

    return clip;
  }

  async playingAnim() {
    this.node.addComponent(Animation);
    this.anim = this.node.getComponent(Animation);
    let clip = await this.getClip();

    this.anim.node = this.node;
    this.anim.name = this.animationName;
    this.anim.playOnLoad = false;

    this.anim.addClip(clip);
    console.log("anim", this.anim);
    console.log("clip", clip);
  }

  checkOutEnable() {
    this.anim.enabled = this.isPlaySign;
  }

  executePlay() {
    this.anim.play(this.clipName);
  }
}
